//
//  UIColor+Extension.swift
//  AispeechMobileApp
//
//  Created by Lee on 2019/2/25.
//  Copyright © 2019年 Lee. All rights reserved.
//

import Foundation
import UIKit

// 十六进制颜色转换
extension UIColor {
    class func hexInt(_ hexValue: Int , alpha: CGFloat? = 1.0) -> UIColor {
        return UIColor(red: ((CGFloat)((hexValue & 0xFF0000) >> 16)) / 255.0,

                       green: ((CGFloat)((hexValue & 0xFF00) >> 8)) / 255.0,

                       blue: ((CGFloat)(hexValue & 0xFF)) / 255.0,

                       alpha: alpha!)
    }
    
  
    convenience init(hexString:String){
        //处理数值
        var cString = hexString.uppercased().trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        
        let length = (cString as NSString).length
        //错误处理
        if (length < 6 || length > 7 || (!cString.hasPrefix("#") && length == 7)){
            //返回whiteColor
            self.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
            return
        }
        
        if cString.hasPrefix("#"){
            cString = (cString as NSString).substring(from: 1)
        }
        
        //字符chuan截取
        var range = NSRange()
        range.location = 0
        range.length = 2
        
        let rString = (cString as NSString).substring(with: range)
        
        range.location = 2
        let gString = (cString as NSString).substring(with: range)
        
        range.location = 4
        let bString = (cString as NSString).substring(with: range)
        
        //存储转换后的数值
        var r:UInt32 = 0,g:UInt32 = 0,b:UInt32 = 0
        //进行转换
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        //根据颜色值创建UIColor
        self.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1.0)
    }
}

// 初始化RGB颜色
extension UIColor {
    convenience init(r : CGFloat, g : CGFloat, b : CGFloat, alpha: CGFloat = 1.0) {
        self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: alpha)
    }
}

// 创建随机色
extension UIColor {
    class func kArc4randomColor() -> UIColor {
        return UIColor.init(r: CGFloat(arc4random_uniform(255)), g: CGFloat(arc4random_uniform(255)), b: CGFloat(arc4random_uniform(255)))
    }
}

//extension UIColor {
//
//    convenience init(hex: String) {
//        self.init(hex: hex, alpha:1)
//    }
//
//    convenience init(hex: String, alpha: CGFloat) {
//        var hexWithoutSymbol = hex
//        if hexWithoutSymbol.hasPrefix("#") {
//            hexWithoutSymbol = hex.substring(1)
//        }
//
//        let scanner = Scanner(string: hexWithoutSymbol)
//        var hexInt:UInt32 = 0x0
//        scanner.scanHexInt32(&hexInt)
//
//        var r:UInt32!, g:UInt32!, b:UInt32!
//        switch (hexWithoutSymbol.length) {
//        case 3: // #RGB
//            r = ((hexInt >> 4) & 0xf0 | (hexInt >> 8) & 0x0f)
//            g = ((hexInt >> 0) & 0xf0 | (hexInt >> 4) & 0x0f)
//            b = ((hexInt << 4) & 0xf0 | hexInt & 0x0f)
//            break;
//        case 6: // #RRGGBB
//            r = (hexInt >> 16) & 0xff
//            g = (hexInt >> 8) & 0xff
//            b = hexInt & 0xff
//            break;
//        default:
//            // TODO:ERROR
//            break;
//        }
//
//        self.init(
//            red: (CGFloat(r)/255),
//            green: (CGFloat(g)/255),
//            blue: (CGFloat(b)/255),
//            alpha:alpha)
//    }
//}

